package may.qf.controller;

import may.qf.entity.Dept;
import may.qf.entity.Emp;
import may.qf.service.DeptService;
import may.qf.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/empList")
    public String getEmpList(Model model, HttpSession session) {
        List<Emp> empList = empService.queryAll();
        model.addAttribute("empList", empList);
        List<Dept> deptList = deptService.queryAll();
        session.setAttribute("deptList",deptList);
        return "emp/empList";
    }

    @RequestMapping(value = "/addEmp", method = RequestMethod.POST)
    public String addEmp(Emp emp) {
        System.out.println("emp = " + emp);
        int insert = empService.insert(emp);
        System.out.println(insert);
        return "redirect:empList";
    }

    @RequestMapping("/delEmp")
    public String delEmp(Integer empNo) {
        System.out.println("EmpController.delEmp");
        int delete = empService.delete(empNo);
        System.out.println("delete = " + delete);
        return "redirect:empList";
    }

    @RequestMapping(value = "/updateEmp", method = RequestMethod.POST)
    public String updateEmp(Emp emp) {
        System.out.println("修改之后  " + emp);
        int update = empService.update(emp);
        System.out.println("update = " + update);
        return "redirect:empList";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Model model, Integer empNo) {
        Emp emp = empService.queryByEmpNo(empNo);
        System.out.println("修改之前：" + emp);
        model.addAttribute("empInfo", emp);
        return "emp/update";
    }

}
